using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Corridors : MonoBehaviour 
{
	public void Corridor(BSPNode areaA, BSPNode areaB)
        {
            corridor cor = new corridor(Map);
	
            if (areaA.roomID.Bottom < areaB.roomID.Top || areaB.roomID.Bottom < areaA.roomID.Top)
            {
                BSPNode uarea = areaA.roomID.Bottom < areaB.roomID.Top;
                BSPNode larea = uarea == areaA ;

                int minAllowanceX = Math.Max(uarea.roomID.Left, larea.roomID.Left);
                int maxAllowanceX = Math.Min(uarea.roomID.Right, larea.roomID.Right);
                if (maxAllowanceX - minAllowanceX >= 4)
                {
                    int corridorX = minAllowanceX + 1 + Rand.Next(maxAllowanceX - minAllowanceX - 2);
                    cor.c(corridorX, uarea.roomID.Bottom, Direction.Up, 0, true);
                    cor.c(corridorX, uarea.roomID.Bottom + 1, Direction.Down, larea.roomID.Top - (uarea.roomID.Bottom + 1), true);
                }
                
            else
            {
                BSPNode leftarea = (areaA.roomID.Right < areaB.roomID.Left) ? areaA : areaB;
                BSPNode rightarea = (leftarea == areaA) ? areaB : areaA;
                
				int minAllowanceY = Math.Max(leftarea.roomID.Top, rightarea.roomID.Top);
                int maxAllowanceY = Math.Min(leftarea.roomID.Bottom, rightarea.roomID.Bottom);
                if (maxAllowanceY - minAllowanceY >= 4)
                {
                    int corridorY = minAllowanceY + 1 + Rand.Next(maxAllowanceY - minAllowanceY - 2);
                    cor.c(leftarea.roomID.Right, corridorY, Direction.Left, 0, true);
                    cor.c(leftarea.roomID.Right + 1, corridorY, Direction.Right, rightarea.roomID.Left - (leftarea.roomID.Right + 1), true);
                }
                
                }
            }
	}
}